<?php
/**
 * @see Alinks_ImportExport_Export_Abstract
 */
require_once('Alinks/ImportExport/Export/Abstract.php');

/**
 * Utility class for exporting the defined keyphrases in the
 * Microsoft Excel XML format
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: ExcelXml.php 390 2008-07-03 05:40:51Z sean $
 * @package Alinks_ImportExport
 */
class Alinks_ImportExport_Export_Excelxml extends Alinks_ImportExport_Export_Abstract
{
	const DATE_FORMAT = 'D, F j, Y';
	
	/**
	 * Returns the keyphrases in the Excel XML format
	 *
	 * @return string
	 */
	public function export()
	{
		$doc = new DOMDocument('1.0', 'UTF-8');
		$doc->appendChild(new DOMComment('Generated by aLinks v' . Alinks_Registry::get('alinks_version') . ' on ' . date('r')));
		$workbook = $doc->appendChild(new DOMElement('Workbook', null, 'urn:schemas-microsoft-com:office:spreadsheet'));
		$workbook->appendChild(new DOMAttr('xmlns:ss', 'urn:schemas-microsoft-com:office:spreadsheet'));
		$this->_buildHead($workbook);
		
		
		$worksheet = $workbook->appendChild(new DOMElement('Worksheet'));
		$worksheet->appendChild(new DOMAttr('ss:Name', 'Sheet1'));
		$table = $worksheet->appendChild(new DOMElement('Table'));
		$table->appendChild(new DOMAttr('ss:StyleID', 'ta1'));
		$table->appendChild(new DOMElement('Column'))->appendChild(new DOMAttr('ss:Width', '150'));
		$table->appendChild(new DOMElement('Column'))->appendChild(new DOMAttr('ss:Width', '230'));
		$table->appendChild(new DOMElement('Column'))->appendChild(new DOMAttr('ss:Width', '120'));
		$table->appendChild(new DOMElement('Column'))->appendChild(new DOMAttr('ss:Width', '250'));
		$table->appendChild(new DOMElement('Column'))->appendChild(new DOMAttr('ss:Width', '120'));
		$table->appendChild(new DOMElement('Column'))->appendChild(new DOMAttr('ss:Width', '420'));
		$this->_exportKeyphrases($table);
		
		$doc->formatOutput = true;
		return $doc->saveXML();
	}
	
	/**
	 * Returns the MIME type for the exported document
	 *
	 * @return string
	 */
	public function getMimeType()
	{
		return 'text/xml';
	}
	
	/**
	 * Returns the file extension for the exported document
	 *
	 * @return string
	 */
	public function getFileExt()
	{
		return 'xml';
	}
	
	protected function _exportKeyphrases(DOMElement $table)
	{
		$this->_addRow($table, 'Phrase', 'Description', 'Factory', 'Options', 'Created', 'Hash', true);
		if (empty($this->_options[self::EXPORT_HASHES])) {
			$keyphrases = Alinks_Registry::get('Alinks_Keyphrase_Keeper')->fetchAll();
		} else {
			$keyphrases = Alinks_Registry::get('Alinks_Keyphrase_Keeper')->fetchByHashes($this->_options[self::EXPORT_HASHES]);
		}
		
		foreach($keyphrases as $k) {
			$this->_addRow(
				$table,
				$k->getPhrase(),
				$k->getDescription(),
				$k->getFactory(),
				$this->_getOptionsString($k->getOptions()),
				date(self::DATE_FORMAT, $k->getCreated()),
				$k->getHash()
			);
		}
	}
	
	/**
	 * Adds a row to the table
	 *
	 * @param DOMElement $table The table
	 */
	protected function _addRow(DOMElement $table, $phrase, $description, $factory, $options, $created, $hash, $heading = false)
	{
		$row = $table->appendChild(new DOMElement('Row'));
		$row->appendChild(new DOMAttr('ss:AutoFitHeight', '0'));
		$row->appendChild(new DOMAttr('ss:Height', '12.8376'));
		
		$cell = $row->appendChild(new DOMElement('Cell'));
		if (!$heading) {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'Default'));
		} else {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'ce1'));
		}
		$cell->appendChild(new DOMElement('Data', htmlspecialchars($phrase)))->appendChild(new DOMAttr('ss:Type', 'String'));
		
		$cell = $row->appendChild(new DOMElement('Cell'));
		if (!$heading) {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'Default'));
		} else {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'ce1'));
		}
		$cell->appendChild(new DOMElement('Data', htmlspecialchars($description)))->appendChild(new DOMAttr('ss:Type', 'String'));
		
		$cell = $row->appendChild(new DOMElement('Cell'));
		if (!$heading) {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'Default'));
		} else {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'ce1'));
		}
		$cell->appendChild(new DOMElement('Data', $factory))->appendChild(new DOMAttr('ss:Type', 'String'));
		
		$cell = $row->appendChild(new DOMElement('Cell'));
		if (!$heading) {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'Default'));
		} else {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'ce1'));
		}
		$cell->appendChild(new DOMElement('Data', htmlspecialchars($options)))->appendChild(new DOMAttr('ss:Type', 'String'));
		
		$cell = $row->appendChild(new DOMElement('Cell'));
		if (!$heading) {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'ce2'));
		} else {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'ce1'));
		}
		$cell->appendChild(new DOMElement('Data', $created))->appendChild(new DOMAttr('ss:Type', 'String'));
		
		$cell = $row->appendChild(new DOMElement('Cell'));
		if (!$heading) {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'Default'));
		} else {
			$cell->appendChild(new DOMAttr('ss:StyleID', 'ce1'));
		}
		$cell->appendChild(new DOMElement('Data', $hash))->appendChild(new DOMAttr('ss:Type', 'String'));
	}
	
	/**
	 * Builds the head of the XML document
	 *
	 * @param DOMElement $workbook The root of the XML document
	 */
	protected function _buildHead(DOMElement $workbook)
	{
		$settings = $workbook->appendChild(new DOMElement('OfficeDocumentSettings', null, 'urn:schemas-microsoft-com:office:office'));
		$this->_addColors($settings);
		$excel_workbook = $workbook->appendChild(new DOMElement('ExcelWorkbook', null, 'urn:schemas-microsoft-com:office:excel'));
		$excel_workbook->appendChild(new DOMElement('WindowHeight', 9000));
		$excel_workbook->appendChild(new DOMElement('WindowWidth', 13860));
		$excel_workbook->appendChild(new DOMElement('WindowTopX', 240));
		$excel_workbook->appendChild(new DOMElement('WindowTopY', 75));
		$excel_workbook->appendChild(new DOMElement('ProtectStructure', 'False'));
		$excel_workbook->appendChild(new DOMElement('ProtectWindows', 'False'));
		$this->_addStyles($workbook);
		
	}
	
	/**
	 * Adds the colors section to the head
	 *
	 * @param DOMElement $settings The settings element
	 */
	protected function _addColors(DOMElement $settings)
	{
		$colors = $settings->appendChild(new DOMElement('Colors'));
		$color = $colors->appendChild(new DOMElement('Color'));
		$color->appendChild(new DOMElement('Index', 3));
		$color->appendChild(new DOMElement('RGB', '#c0c0c0'));
		
		$color = $colors->appendChild(new DOMElement('Color'));
		$color->appendChild(new DOMElement('Index', 4));
		$color->appendChild(new DOMElement('RGB', '#e6e6ff'));
		
		$color = $colors->appendChild(new DOMElement('Color'));
		$color->appendChild(new DOMElement('Index', 5));
		$color->appendChild(new DOMElement('RGB', '#ff0000'));
	}
	
	/**
	 * Adds the styles to the document
	 *
	 * @param DOMElement $workbook The root eleement
	 */
	protected function _addStyles(DOMElement $workbook)
	{
		
		
		$styles = $workbook->appendChild(new DOMElement('Styles'));
		$style = $styles->appendChild(new DOMElement('Style'));
		$style->appendChild(new DOMAttr('ss:ID', 'Default'));
		$style->appendChild(new DOMAttr('ss:Name', 'Default'));
		
		$style = $styles->appendChild(new DOMElement('Style'));
		$style->appendChild(new DOMAttr('ss:ID', 'Result'));
		$style->appendChild(new DOMAttr('ss:Name', 'Result'));
		$font = $style->appendChild(new DOMElement('Font'));
		$font->appendChild(new DOMAttr('ss:Bold', '1'));
		$font->appendChild(new DOMAttr('ss:Italic', '1'));
		$font->appendChild(new DOMAttr('ss:Size', '10'));
		$font->appendChild(new DOMattr('ss:Underline', 'Single'));
		
		$style = $styles->appendChild(new DOMElement('Style'));
		$style->appendChild(new DOMAttr('ss:ID', 'Result2'));
		$style->appendChild(new DOMAttr('ss:Name', 'Result2'));
		$font = $style->appendChild(new DOMElement('Font'));
		$font->appendChild(new DOMAttr('ss:Bold', '1'));
		$font->appendChild(new DOMAttr('ss:Italic', '1'));
		$font->appendChild(new DOMAttr('ss:Size', '10'));
		$font->appendChild(new DOMattr('ss:Underline', 'Single'));
		$number = $style->appendChild(new DOMElement('NumberFormat'));
		$number->appendChild(new DOMAttr('ss:Format', 'Currency'));
		
		$style = $styles->appendChild(new DOMElement('Style'));
		$style->appendChild(new DOMAttr('ss:ID', 'Heading'));
		$style->appendChild(new DOMAttr('ss:Name', 'Heading'));
		$style->appendChild(new DOMElement('Alignment'))->appendChild(new DOMAttr('ss:Horizontal', 'Center'));
		$font = $style->appendChild(new DOMElement('Font'));
		$font->appendChild(new DOMAttr('ss:Bold', '1'));
		$font->appendChild(new DOMAttr('ss:Italic', '1'));
		$font->appendChild(new DOMAttr('ss:Size', '10'));
		
		$style = $styles->appendChild(new DOMElement('Style'));
		$style->appendChild(new DOMAttr('ss:ID', 'Heading1'));
		$style->appendChild(new DOMAttr('ss:Name', 'Heading1'));
		$style->appendChild(new DOMElement('Alignment'))->appendChild(new DOMAttr('ss:Horizontal', 'Center'));
		$font = $style->appendChild(new DOMElement('Font'));
		$font->appendChild(new DOMAttr('ss:Bold', '1'));
		$font->appendChild(new DOMAttr('ss:Italic', '1'));
		$font->appendChild(new DOMAttr('ss:Size', '10'));
		
		$styles->appendChild(new DOMElement('Style'))->appendChild(new DOMAttr('ss:ID', 'co1'));
		$styles->appendChild(new DOMElement('Style'))->appendChild(new DOMAttr('ss:ID', 'co2'));
		$styles->appendChild(new DOMElement('Style'))->appendChild(new DOMAttr('ss:ID', 'co3'));
		$styles->appendChild(new DOMElement('Style'))->appendChild(new DOMAttr('ss:ID', 'co4'));
		$styles->appendChild(new DOMElement('Style'))->appendChild(new DOMAttr('ss:ID', 'co5'));
		$styles->appendChild(new DOMElement('Style'))->appendChild(new DOMAttr('ss:ID', 'co6'));
		$styles->appendChild(new DOMElement('Style'))->appendChild(new DOMAttr('ss:ID', 'co7'));
		$styles->appendChild(new DOMElement('Style'))->appendChild(new DOMAttr('ss:ID', 'ta1'));
		
		$style = $styles->appendChild(new DOMElement('Style'));
		$style->appendChild(new DOMAttr('ss:ID', 'ce1'));
		$interior = $style->appendChild(new DOMElement('Interior'));
		$interior->appendChild(new DOMAttr('ss:Color', '#e6e6ff'));
		$interior->appendChild(new DOMAttr('ss:Pattern', 'Solid'));
		$font = $style->appendChild(new DOMElement('Font'));
		$font->appendChild(new DOMAttr('ss:Bold', '1'));
		$font->appendChild(new DOMAttr('ss:Size', '10'));
		
		$style = $styles->appendChild(new DOMElement('Style'));
		$style->appendChild(new DOMAttr('ss:ID', 'ce2'));
		$number = $style->appendChild(new DOMElement('NumberFormat'));
		$number->appendChild(new DOMAttr('ss:Format', 'Medium Date'));
	}
	
	/**
	 * Returns the keyphrase options as a string
	 *
	 * @param array $options The keyphrase options
	 * @return string
	 */
	protected function _getOptionsString(array $options)
	{
		$o = array();
		foreach($options as $key => $value) {
			$o[] = '"' . $key . '" = "' . $value . '"';
		}
		
		return join(', ', $o);
	}
}